package com.uliky.shouyiLibrary.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.uliky.common.constant.AuthServerConstant;
import com.uliky.common.utils.R;
import com.uliky.common.vo.MemberRespVo;
import com.uliky.shouyiLibrary.auth.feign.MemberFeignService;
import com.uliky.shouyiLibrary.auth.util.HttpUtils;
import com.uliky.shouyiLibrary.auth.vo.SocialUser;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 社交登录请求
 */
@Controller
public class OAuth2Controller {
    @Autowired
    MemberFeignService memberFeignService;
    @GetMapping("/oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {
        //根据code换取token
        Map<String,String> map = new HashMap<>();
        map.put("grant_type","authorization_code");  //授权方式
        map.put("code",code);//code
        map.put("client_id","68bb0dceb407f4213baff7a7f399f845296314f9929e0dc23ef30c4f243dbabe"); //appid
        map.put("redirect_uri","http://auth.shouyiLibrary.com/oauth2.0/weibo/success"); //回调地址
        map.put("client_secret","c568b6a9b2716fd266bbe9d4a8de254144a5509db5b61626245ef92e25eef258");

        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), new HashMap<>(), map);
        //POST https://gitee.com/oauth/
        //    token?grant_type=authorization_code&
        //    code=c9852f499ef4f04fa811df3ee342e7823e2291159f90acc45e5b404447dc8d09&
        //    client_id=68bb0dceb407f4213baff7a7f399f845296314f9929e0dc23ef30c4f243dbabe&
        //    redirect_uri=http://shouyiLibrary.com/oauth2.0/weibo/success&
        //    client_secret=c568b6a9b2716fd266bbe9d4a8de254144a5509db5b61626245ef92e25eef258

        //处理的到token
        if(response.getStatusLine().getStatusCode() == 200) {
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            //1.判断当前用户是不是第一次登录
            R oauthLogin = memberFeignService.oauthLogin(socialUser);
            if(oauthLogin.getCode() == 0) {
                //提取用户信息
                MemberRespVo memberRespVo = oauthLogin.getData("data", new TypeReference<MemberRespVo>() {});
                session.setAttribute(AuthServerConstant.LOGIN_USER,memberRespVo);
                //登陆成功，跳转到首页
                return "redirect:http://shouyiLibrary.com";
            }else {
                //登陆失败，跳转到登录页
                return "redirect:http://shouyiLibrary.com/login.html";
            }
        }else {
            //登陆失败，跳转到登录页
            return "redirect:http://shouyiLibrary.com/login.html";
        }
    }
}
